//
//  WSJShowNewfeatureTool.m
//  LCNewFeatureDemo
//
//  Created by wsj2015 on 16/7/29.
//  Copyright © 2016年 Leo. All rights reserved.
//

#import "WSJShowNewfeatureTool.h"
#import "LCNewFeatureVC.h"

#import "UploadFormVC.h"
#import "MMDrawerController.h"
#import "LeftMenuVC.h"


// 屏幕尺寸
#define SCREEN_SIZE [UIScreen mainScreen].bounds.size
static UIWindow *mywindow = nil;
@implementation WSJShowNewfeatureTool


+ (void)handleConfigWithWindow:(UIWindow *)window{

   
    window.backgroundColor = [UIColor whiteColor];
    mywindow = window;
#pragma mark 1. 是否应该显示新特性界面
    
    BOOL showNewFeature = [LCNewFeatureVC shouldShowNewFeature];
    
    //写死  调试
//    showNewFeature = YES;
    if (showNewFeature == NO) {
        [WSJShowNewfeatureTool restoreRootViewController];
    }else{
        
        
        // 进入主界面按钮
        UIButton *enterBtn = [[UIButton alloc] init];
        [enterBtn setBackgroundColor:[UIColor whiteColor]];
        [enterBtn setTitle:@"立即体验" forState:UIControlStateNormal];
        CGFloat y  = (kScreenHeight-110);
        if (kScreenWidth == 320 && kScreenHeight ==480) {//iphone4
            
        }else if(kScreenWidth == 320 && kScreenHeight ==568){ //iphone 5
            y  = (kScreenHeight-110);
        }else if(kScreenWidth == 375 && kScreenHeight == 667){ //iphone 6
            
        }else if(kScreenWidth == 414 && kScreenHeight == 736){ //iphone 6P
            y  = (kScreenHeight-110);
        }
        
        
        [enterBtn setFrame:(CGRect){(kScreenWidth - 140)/2,y, 140, 44.0f}];
        [enterBtn setTitleColor:kMainColor forState:UIControlStateNormal];
        enterBtn.layer.borderWidth = 1;
        enterBtn.layer.borderColor = [UIColor whiteColor].CGColor;
        wsj_cornerRadius(enterBtn, 8);
        
        [enterBtn addTarget:self action:@selector(restoreRootViewController) forControlEvents:UIControlEventTouchUpInside];
        
        LCNewFeatureVC *newFeatureVC = [LCNewFeatureVC newFeatureWithImageName:@"new_feature"
                                                                    imageCount:3
                                                               showPageControl:YES
                                                                   enterButton:enterBtn];
        newFeatureVC.showSkip = YES;
        newFeatureVC.skipBlock = ^(void) {
//            [WSJShowNewfeatureTool enterMainVC];
        };
      
        
#pragma mark 2.2. 方式二: 一直左划进入主界面
        
        /*
         LCNewFeatureVC *newFeatureVC = [LCNewFeatureVC newFeatureWithImageName:@"new_feature"
         imageCount:3
         showPageControl:YES
         finishBlock:^{
         
         [weakSelf enterMainVC];
         }];
         */
        
#pragma mark 3. 设置新特性界面的属性(可选步骤)
        
        // 当前页码的代理
//        newFeatureVC.delegate = self;
        
        // 当前点颜色, 默认[UIColor darkGrayColor]
        newFeatureVC.pointCurrentColor = kMainColor;
        newFeatureVC.showSkip = NO;
        // 其他点颜色, 默认[UIColor lightGrayColor]
         newFeatureVC.pointOtherColor = HexRGB_WSJ(115, 167, 190);
        
        // 状态栏样式, 默认LCStatusBarStyleBlack
        // newFeatureVC.statusBarStyle = LCStatusBarStyleWhite;
        
#pragma mark 4. 设置新特性界面为当前窗口的根视图控制器
        
        mywindow.rootViewController = newFeatureVC;
        

    }
    [mywindow makeKeyAndVisible];

}
#pragma mark - 进入主界面

+ (void)enterMainVC {
    
    UINavigationController *nav= [[UINavigationController alloc] initWithRootViewController:[UploadFormVC new]];
    MMDrawerController * drawerController = [[MMDrawerController alloc]
                                             initWithCenterViewController:nav
                                             leftDrawerViewController:[LeftMenuVC new]
                                             rightDrawerViewController:nil];
    
    mywindow.rootViewController  = drawerController;
    drawerController.showsShadow = YES;
    [drawerController setMaximumLeftDrawerWidth:80];
    [drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
    [drawerController setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];
    
  
   
}

+ (void)restoreRootViewController {
    
    
    //只有当立即体验被点击是时候 才记录版本号
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *key = @"CFBundleShortVersionString";
//    // 获取沙盒中版本号
//    NSString *lastVersion = [defaults stringForKey:key];
    
    // 获取当前的版本号
    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[key];
  
    [defaults setObject:currentVersion forKey:key];
    [defaults synchronize];
    
    [UIView transitionWithView:mywindow duration:0.7f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
        
        BOOL oldState = [UIView areAnimationsEnabled];
        [UIView setAnimationsEnabled:NO];
        [self enterMainVC];
        [UIView setAnimationsEnabled:oldState];
        
    } completion:nil];
}
@end
